package com.szxb.facebus.util.basics;

import android.os.Looper;
import android.text.TextUtils;

import com.szxb.mlog.SLog;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.text.DecimalFormat;

/**
 * 作者：Tangren on 2018-04-12
 * 包名：com.szxb.jcbus.util
 * 邮箱：996489865@qq.com
 * TODO:一句话描述
 */

public class Utils {

    /**
     * @return 是否只主线程
     */
    public static boolean isMainThread() {
        return Looper.getMainLooper() == Looper.myLooper();
    }


    /**
     * Str转int
     *
     * @param str .
     * @return .
     */
    public static int String2Int(String str) {
        try {
            return Integer.valueOf(str);
        } catch (Exception e) {
            e.printStackTrace();
            SLog.e("Utils(String2Int.java:41)" + Utils.getExectionStr(e));
            return 0;
        }
    }

    /**
     * Str转int
     *
     * @param str .
     * @return .
     */
    public static int hexString2Int(String str) {
        try {
            return Integer.parseInt(str, 16);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 基础票价
     *
     * @param priceBase
     * @return
     */
    public static String priceBaseIdeal(int priceBase) {
        DecimalFormat format = new DecimalFormat("0.00");
        return format.format((float) priceBase / (float) 100);
    }


    /**
     * 分转元，精确度为小数点后两位
     *
     * @param value
     * @return
     */
    public static String getvalue(long value) {
        DecimalFormat df = new DecimalFormat("#.00");
        String fb = df.format(value);
        Double db = Double.parseDouble(fb) / 100;
        //    Log.e("ddd", "getvalue: db = "+db );
        return db.toString();
    }

    /**
     * 检查字符串是否可以转化成数字
     *
     * @param str
     * @return
     */
    public static boolean isNumber(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        } else {
            char[] chars = str.toCharArray();
            int sz = chars.length;
            boolean hasExp = false;
            boolean hasDecPoint = false;
            boolean allowSigns = false;
            boolean foundDigit = false;
            int start = chars[0] == 45 ? 1 : 0;
            int i;
            if (sz > start + 1 && chars[start] == 48 && chars[start + 1] == 120) {
                i = start + 2;
                if (i == sz) {
                    return false;
                } else {
                    while (i < chars.length) {
                        if ((chars[i] < 48 || chars[i] > 57) && (chars[i] < 97 || chars[i] > 102) && (chars[i] < 65 || chars[i] > 70)) {
                            return false;
                        }
                        ++i;
                    }
                    return true;
                }
            } else {
                --sz;
                for (i = start; i < sz || i < sz + 1 && allowSigns && !foundDigit; ++i) {
                    if (chars[i] >= 48 && chars[i] <= 57) {
                        foundDigit = true;
                        allowSigns = false;
                    } else if (chars[i] == 46) {
                        if (hasDecPoint || hasExp) {
                            return false;
                        }

                        hasDecPoint = true;
                    } else if (chars[i] != 101 && chars[i] != 69) {
                        if (chars[i] != 43 && chars[i] != 45) {
                            return false;
                        }

                        if (!allowSigns) {
                            return false;
                        }

                        allowSigns = false;
                        foundDigit = false;
                    } else {
                        if (hasExp) {
                            return false;
                        }
                        if (!foundDigit) {
                            return false;
                        }
                        hasExp = true;
                        allowSigns = true;
                    }
                }
                return i < chars.length ? (chars[i] >= 48 && chars[i] <= 57 || ((chars[i] != 101 && chars[i] != 69) && (chars[i] == 46 ? ((!hasDecPoint && !hasExp) && foundDigit) : (allowSigns || chars[i] != 100 && chars[i] != 68 && chars[i] != 102 && chars[i] != 70 ? (!(chars[i] != 108 && chars[i] != 76) && (foundDigit && !hasExp && !hasDecPoint)) : foundDigit)))) : !allowSigns && foundDigit;
            }
        }
    }


    public static String tip() {
        return tip[(int) (Math.random() * 20)];
    }

    //交通提示语
    private static String[] tip = new String[]{
            "安全靠着你我他，和谐交通靠大家！",
            "你争我抢道路窄，互谦互让心路宽！",
            "与文明一起上路，伴平安一起回家！",
            "人人需要文明交通，交通需要人人文明！",
            "用礼让传递文明，用安全构筑和谐！",
            "出行因礼让而畅通，道路因畅通而和谐！",
            "一路文明一路情，用安全构筑和谐！",
            "带十分小心上路，携一份平安回家！",
            "出行多点小心，家人少点担心！",
            "冒险是事故之根，谨慎为安全之本！",
            "文明在于一言一行，安全源于一点一滴",
            "创优良交通秩序，闪精神文明之光！",
            "交通安全系万家，平平安安是幸福！",
            "安全开车是大事，文明走路非小节！",
            "红灯常在心中亮，绿灯才能伴一生！",
            "树立安全第一的思想，落实预防为主的方针",
            "多一分麻痹，多一分危险。多一些谨慎，多一些安全！",
            "交通安全系万家，平平安安是幸福！",
            "创优良交通秩序，闪精神文明之光！",
            "文明在于一言一行，安全源于一点一滴！"
    };


    /**
     * @param source .
     * @param start  ，
     * @param end    .
     * @return 是否在此范围
     */
    public static boolean range(int source, int start, int end) {
        return (source >= start && source < end);
    }


    /**
     * @param e .
     * @return 异常具体信息
     */
    public static String getExectionStr(Exception e) {
        try {
            if (e == null) {
                return "未知错误UNK";
            }
            Writer writer = new StringWriter();
            PrintWriter pw = new PrintWriter(writer);
            e.printStackTrace(pw);
            Throwable cause = e.getCause();
            while (cause != null) {
                cause.printStackTrace(pw);
                cause = cause.getCause();
            }
            pw.close();
            return writer.toString();
        } catch (Exception ex) {
            ex.printStackTrace();
            SLog.d("Util(getExectionStr.java:501)异常转换异常");
            return ex.toString();
        }

    }


    /**
     * 随机字符串
     *
     * @param length
     * @return
     */
    public static String Random(int length) {
        char[] ss = new char[length];
        int i = 0;
        while (i < length) {
            int f = (int) (Math.random() * 5);
            if (f == 0)
                ss[i] = (char) ('A' + Math.random() * 26);
            else if (f == 1)
                ss[i] = (char) ('a' + Math.random() * 26);
            else
                ss[i] = (char) ('0' + Math.random() * 10);
            i++;
        }
        return String.valueOf(ss);
    }
}
